var  BASE = BASE||{}; //全局公用方法
$(function () {
    BASE.COMMON.urlLeftNav();
    BASE.COMMON.recommendDarenList(1,10);
    $(document).scroll(function () {
        BASE.COMMON.scrollEvent(BASE.SOURCE.getData);
    });
});

BASE.SOURCE = (function () {
    var PAGE = 2;
    /*弹出层*/
    function showPopup(obj) {
        BASE.POPUP.SELECTEDCOVERS = [];
        BASE.POPUP.initShowCovers();
        $('#title').val('');
        $('#price').val('');
        BASE.UPLOADANSWER.ue.txt.html('');
        $('#order_price').val('');
        var userId = BASE.COMMON.getUserId();
        if(!userId){
            location.href = '/login.html';
            return;
        }
        var flag = $(obj).data('flag');
        if(flag){
            $('#answer_pop').children('.pop-answer').removeClass('is-full-screen');
            $('#answer_pop').show();
            return;
        }
        $(obj).data('flag',true);
        setTimeout(function () {
            $(obj).data('flag',false);
        },500);
        BASE.POPUP.getInterest('',function () {
            $('#answer_pop').show();
            $(obj).data('flag',true);
        },true);
    }

    /*发单*/
    function sendOrder(obj) {
        var sendData = _getAddData();
        if(!sendData){
            return;
        }
        $(obj).attr('disabled',true);
        BASE.postHttp('/device/sourceCode/add',sendData,function (res) {
            if(res.code != 200){
                BASE.clearpop(res.message || '新增失败');
                $(obj).attr('disabled',false);
                return;
            }
            $('#answer_pop').hide();
            window.location.reload();
        })
    }

    /*获取新增的数据*/
    function _getAddData() {
        var payTypermb = $('input[name="pay-type-rmb"]:checked').val() || '';
        var payTypegold = $('input[name="pay-type-gold"]:checked').val() || '';
        var linkIcon = payTypermb && payTypegold ? ',' : '';
        var sendData = {
            title: $('#title').val(),
            content: BASE.UPLOADANSWER.ue.txt.html(),
            covers: BASE.POPUP.getCovers().join(' '),
            categoryId: $('#interestChildData option:selected').val() ||  $('#interestData option:selected').val(),
            attachUrl:$('body').data('file-url'),
            panUrl:$('#fileLink').val(),
            panPwd:$('#filePas').val(),
            price:$('#price').val(),
           /* supportPays:payTypermb + linkIcon + payTypegold, //人民币金币*/
            supportPays:$('input[name="pay-type"]:checked').val(), //人民币金币*/
            replyShowIs:$('#down_type').is(':checked'),
        };
        var payType = $('input[name="price-type"]:checked').val(); //免费收费
        if(payType == 2){ //免费
            sendData.price = 0;
        }
        if(payType == 1){ //收费
            delete sendData.replyShowIs;
        }
        if(!sendData.title){
            BASE.clearpop('请输入项目标题');
            return false;
        }
        if(!sendData.content){
            BASE.clearpop('请输入项目简介');
            return false;
        }
        if(payType == 1){
            if(!sendData.price){
                BASE.clearpop('请输入价格');
                return false;
            }
            if(!sendData.supportPays){
                BASE.clearpop('请选择支付方式');
                return false;
            }
        }
        if(!sendData.panUrl){
            delete sendData.panUrl;
            delete sendData.panPwd;
        }
        if(!sendData.panUrl && !sendData.attachUrl){
            BASE.clearpop('请上传附件或填写网盘链接');
            return false;
        }
        return sendData;
    }
    /*获取推荐的数据*/
    function getData() {
        var $box = $('#middleListContent');
        $box.find('.loading').show();
        var sendData = {
            pageNum:PAGE,
            pageSize:15,
        };
        BASE.apiGet('/device/sourceCode/list',sendData,function (res) {
            ++PAGE;
            $box.find('.loading').hide();
            BASE.COMMON.SCROLLFLAG = true;
            if((res.nextPage == 0 && res.pages > 1) || res.pages <= 1){
                BASE.COMMON.SCROLLFLAG = false;
                if(res.pages!=0 && res.total>=8){
                    $box.find('.nomore-data').show();
                }
            }
            $box.find('.content').append(template('middleListContent_tpl',{data:res.list}));
        })
    }
    return {
        showPopup:showPopup,
        sendOrder:sendOrder,
        getData:getData,
    }
})();